package com.biz.hunter.dao;

import java.util.Collections;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.biz.hunter.db.entity.Employee;
import com.biz.hunter.db.entity.Person;
import com.biz.hunter.db.entity.UserInfo;
import com.biz.hunter.dbutil.BaseDAO;


@Repository
public class PersonDAO_TEMP {
	protected static final String SQL_SAVE = "insert into tb_person(openid,name,age,workage,"
			+ "phone,address,email,icon,"
			+ "image,bestdomain,active,createDate,"
			+ "inservice,companyID,  cityId, roleId,"
			+ " companyname, mobile, platformid,domainId) "
			+ "values(?,?,?,?"
			+ ",?,?,?,?"
			+ ",?,?,?,?"
			+ ",?,?,?,?"
			+ ",?,?,?,?)";

	protected static final String SQL_UPDATE = "update tb_person set openid=?, name=?" +
			",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? "
			+ ", active=?, createDate=? ,inservice=?,companyID=?, cityid=?, roleid=?, companyname=?, mobile=?, "
			+ "roleName = ?, platformid=? where id =?";
	
	protected static final String SQL_GET_OPENID = "select * from tb_person where openid = ?";
	
	protected static final String SQL_GET_ALL = "select * from tb_person ";
	
	protected static final String SQL_GET_BY_PHONE = "select * from tb_person where phone = ?";
	
	protected static final String SQL_GET_LIST = "select * from tb_person where id in (";
	
	protected static final String SQL_GET = "select * from tb_person  where id = ?";
	
	protected static final String SQL_DELETE = "delete from tb_person where id=?";
	
	public int save(Person person) {
		if (person == null) {
			return -1;
		}
		Long id = BaseDAO.save(SQL_SAVE, person.getOpenid(),
				person.getName(),person.getAge(),person.getWorkage(),person.getPhone(),
				person.getAddress(), person.getEmail(),person.getIcon(),person.getImage(),
				person.getBestdomain(),	person.getActive(),person.getCreateDate(),
				person.getInservice(),person.getCompanyId(),person.getCityId(),person.getRoleId(), 
				person.getCompanyName(), person.getMobile(), person.getPlatformId(),
				person.getDomainId()
				);
	    
		return id.intValue();
	}

	public int delete(String... personIds) {
		if (personIds != null) {
			if (personIds.length == 1) {
				return BaseDAO.execute(SQL_DELETE, personIds[0]);
			} else {
				Object[][] params = new Object[personIds.length][1];
				int i = 0;
				for (String id : personIds) {
					params[i][0] = id;
				}

				int[] rows = BaseDAO.batch(SQL_DELETE, params);
				if (rows != null && rows.length > 0) {
					int total = 0;
					for (int r : rows) {
						total += r;
					}
					return total;
				} else {
					return -1; 
				}
			}
		} else {
			return 0;
		}
	}

	public List<Person> get(String... Ids) {
		if (Ids != null) {
			StringBuilder sql = new StringBuilder(SQL_GET_LIST);
			boolean start = false;
			for (String id : Ids) {
				if (start) {
					sql.append(",").append(id);
				} else {
					start = true;
					sql.append(id);
				}
			}
			sql.append(")");
			return BaseDAO.queryForList(Person.class, sql.toString());

		} else {
			return Collections.emptyList();
		}
	}
	
	public List<Person> getALL() {
		return BaseDAO.queryForList(Person.class, SQL_GET_ALL);
	}
	
	protected static final String SQL_UPDATE1 = "update tb_person set openid=?, name=?" +
      ",age=?, workage=?,phone=?,address =?,email=?,icon=?,image=?,bestdomain=? ,"
      + " active=?, createDate=? ,inservice=?, companyID=?, companyname=?, cityid=?,cityname= ?,"
      + " roleid=?, roleName=? , mobile=?, platformid=? where id =?";
  
	public int update(Person person) {
		return BaseDAO.execute(SQL_UPDATE1, person.getOpenid(),person.getName(), person.getAge(),
				person.getWorkage(), person.getPhone(), person.getAddress(),person.getEmail(),
				person.getIcon(),person.getImage(), person.getBestdomain(),
				person.getActive(), person.getCreateDate(),person.getInservice(),
				person.getCompanyId(),person.getCompanyName(),person.getCityId(),person.getCityName(),
				person.getRoleId(),person.getRoleName(), 
				person.getMobile(), person.getPlatformId(), person.getId());
	}
	
	public Person getByOpenId(String openid) {
		return  BaseDAO.queryForObject(Person.class, SQL_GET_OPENID, openid);
	}

	public Person getById(int personId) {
		return  BaseDAO.queryForObject(Person.class, SQL_GET, personId);
	}

	public Person getByPhone(String phone) {
		return  BaseDAO.queryForObject(Person.class, SQL_GET_BY_PHONE, phone);
	}

	public int delete(int personId) {
		return BaseDAO.execute(SQL_DELETE,personId);
	}

	
	 protected static final String SQL_UPDATE_PROFILE = "update tb_person "
	     + " set  name=? ,  workage=?, bestdomain=? ,   "
	     + "companyID=?, companyname=?, cityid=?,cityname= ?,"
	      + " roleid=?, roleName=? , platformid=?,  domainId= ? , domainName=? ,image=? where id =?";
	 
  public int updateProfile(Employee person) {
    return BaseDAO.execute(SQL_UPDATE_PROFILE, person.getName(), 
                           person.getWorkage(),   person.getBestdomain(), 
                           person.getCompanyId(),person.getCompanyName(),person.getCityId(),
                           person.getCityName(), person.getRoleId(),person.getRoleName(), 
                           person.getPlatformId(), person.getDomainId(),person.getDomainName(),person.getImgUrl(),
                           person.getId());
  }

}
